package org.sundatagroup.aurora.core.config;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * <p>处理获取图片位置的配置
 * </p>
 * 
 * @author Zdream
 * @date 2018-04-23
 * @since 0.1.1
 */
@Configuration
public class WebImageConfig extends WebMvcConfigurerAdapter {
	
	@Value("${img.location}")
    private String mImagesPath;
    
    @Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (mImagesPath.equals("") || mImagesPath.equals("${img.location}")) {
			String imagesPath = WebImageConfig.class.getClassLoader().getResource("").getPath();
			if (imagesPath.indexOf(".jar") > 0) {
				imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
			} else if (imagesPath.indexOf("classes") > 0) {
				imagesPath = "file:" + imagesPath.substring(0, imagesPath.indexOf("classes"));
			}
			imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/")) + "/images/";
			mImagesPath = imagesPath;
		}
		LoggerFactory.getLogger(WebImageConfig.class).debug("设置图片保存位置: " + mImagesPath);
		registry.addResourceHandler("/images/**").addResourceLocations(mImagesPath);
		
		super.addResourceHandlers(registry);
	}

}
